home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------
- | FILE NAME: Strings.c
- |
- | DOCUMENT: [1016.0]
- |
- | PURPOSE: To provide a system of string manipulation functions.
- |
- | DESCRIPTION:
- |
- | NOTE: See 'DynString.c' for dynamically allocated string
- | functions.
- |
- | See 'Parse.c' for string parsing functions.
- |
- | HISTORY: 02.03.93 from xstrings.c.
- | 11.08.93 moved parsing functions to 'Parse.c'
- ------------------------------------------------------------*/
-
- #define lower(c) (isupper(c) ? tolower(c) : (c))
-
- #include <ctype.h>
- #include <Strings.h>
-
- /*------------------------------------------------------------
- | NAME: AppendString
- |
- | PURPOSE: To append a string to another.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: AppendString( AString, SuffixString );
- |
- | NOTE:
- |
- | ASSUMES: String is terminated with a 0.
- |
- | HISTORY: 02.01.89 by Lee Malone
- ------------------------------------------------------------*/
- Nothing
- AppendString( AddressOfString AString,
- AddressOfString SuffixString )
- {
- CopyString( SuffixString,
- AString + CountString( AString ) );
- }
-
- /*------------------------------------------------------------
- | NAME: AppendStrings
- |
- | PURPOSE: To append a variable number of strings together.
- |
- | DESCRIPTION:
- |
- | EXAMPLE:
- |
- | String ABuffer[255];
- |
- | CopyString( "This", ABuffer );
- |
- | AppendStrings( ABuffer, <-- destination string
- | " is", <-- AddressOf suffix string
- | " a", <-- AddressOf suffix string
- | " test.", <-- AddressOf suffix string
- | (AddressOfString) 0 ); <-- 0 terminates parameter list
- |
- |
- | yields: "This is a test."
- |
- |
- | NOTE: The terminating 0 parameter must be cast as
- | 'AddressOfString' to prevent errors.
- |
- | ASSUMES: String is terminated with a 0.
- | Destination buffer is large enough to hold result.
- | Parameters are compiled in
- | left-to-right:low-to-high-memory order.
- |
- | HISTORY: 12.06.89 by Lee Malone from source by Jack A. Zucker
- ------------------------------------------------------------*/
- Nothing
- AppendStrings( AddressOfString AString,
- AddressOfString SuffixString, ... )
- {
- AddressOfAddressOfString ParameterPointer;
-
- ParameterPointer = &SuffixString;
-
- while( *ParameterPointer )
- {
- AppendString( AString, *ParameterPointer );
- ParameterPointer++;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: CompareStrings
- |
- | PURPOSE: To compare two strings based on their ordering in
- | the alphabet, independent of upper/lower case.
- |
- | DESCRIPTION: Comparison operation.
- | Returns: 0 if string AA = string BB.
- | positive number if AA > BB.
- | negative number if AA < BB.
- |
- | Compared until end of either string or until an in-equality
- | is detected.
- |
- | EXAMPLE: Result = CompareStrings( Name, "John Galt" );
- |
- |
- | NOTE:
- |
- | ASSUMES: String is terminated with a 0.
- |
- | HISTORY: 01.11.89 by Lee Malone
- | 04.04.91 removed case sensitive comparison
- | 11.11.93 changed name from
- | 'CompareStringsLexigraphic'
- |
- ------------------------------------------------------------*/
- Comparison
- CompareStrings( AddressOfString A,
- AddressOfString B)
- {
- Comparison Result;
-
- while(1)
- {
- Result = (Comparison) (lower(*A) - lower(*B));
-
- /* return if unequal or end of either string */
-
- if(Result || *A == 0 || *B == 0)
- {
- return(Result);
- }
- A++;
- B++;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: ConvertStringToLowerCase
- |
- | PURPOSE: To convert a string in place to lower case.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: ConvertStringToLowerCase( AString );
- |
- | NOTE:
- |
- | ASSUMES: String is terminated with a 0.
- |
- | HISTORY: 03.25.91 by Lee Malone
- | 11.11.93 fixed 'isupper' error
- ------------------------------------------------------------*/
- Nothing
- ConvertStringToLowerCase(AddressOfString AString)
- {
- while(*AString != 0)
- {
- if(isupper(*AString))
- {
- *AString = tolower((int)*AString);
- }
- AString++;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: ConvertStringToUpperCase
- |
- | PURPOSE: To convert a string in place to upper case.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: ConvertStringToUpperCase( AString );
- |
- | NOTE:
- |
- | ASSUMES: String is terminated with a 0.
- |
- | HISTORY: 03.22.89 by Lee Malone
- ------------------------------------------------------------*/
- Nothing
- ConvertStringToUpperCase(AddressOfString AString)
- {
- while(*AString != 0)
- {
- if(islower(*AString))
- {
- *AString = toupper((int)*AString);
- }
- AString++;
- }
- }
-
- /*------------------------------------------------------------
- | NAME: CopyString
- |
- | PURPOSE: To copy a zero-delimited string from one place
- | to another including the delimiter.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: CopyString(FromString,ToBuffer);
- |
- | NOTE:
- |
- | ASSUMES: Source and Target buffers are non-overlapping.
- |
- | HISTORY: 11.14.89 by Lee Malone
- | 02.15.93 changed to quad count.
- | 11.01.93 return value removed; now buffers must
- | not overlap.
- ------------------------------------------------------------*/
- Nothing
- CopyString( AddressOfString Source,
- AddressOfString Target )
- {
- while( *Source != 0 )
- {
- *Target++ = *Source++;
- }
- *Target = 0;
- }
-
- /*------------------------------------------------------------
- | NAME: CountString
- |
- | PURPOSE: To count the data bytes in a 0-terminated string.
- |
- | DESCRIPTION: Terminating 0 is not included in the count.
- |
- | EXAMPLE: ByteCount = CountString( MyString );
- |
- | NOTE:
- |
- | ASSUMES:
- |
- | HISTORY: 08.31.89 by Lee Malone
- | 02.15.93 changed to return Quad instead of Pair.
- ------------------------------------------------------------*/
- Quad
- CountString( AddressOfString AString )
- {
- Quad ByteCount;
-
- ByteCount = 0;
-
- while( AString[ByteCount] != 0 )
- {
- ByteCount++;
- }
-
- return( ByteCount );
- }
-
- /*------------------------------------------------------------
- | NAME: FindLastByteInString
- |
- | PURPOSE: To find the address of the last byte in a string.
- |
- | DESCRIPTION: Returns the address of the byte immediately
- | prior to the first 0 byte. Empty strings
- | return the address of the byte prior to where
- | the first byte would be found.
- |
- | EXAMPLE: LastByteAddress = FindLastByteInString(AString);
- |
- | NOTE:
- |
- | ASSUMES: String is terminated with a 0.
- |
- | HISTORY: 02.01.89 by Lee Malone
- ------------------------------------------------------------*/
- AddressOfString
- FindLastByteInString(AddressOfString AString)
- {
- while (*AString != 0) ++AString;
-
- AString--;
-
- return(AString);
- }
-
- /*------------------------------------------------------------
- | NAME: InsertString
- |
- | PURPOSE: To insert a string into another string.
- |
- | DESCRIPTION:
- |
- | EXAMPLE:
- | String ABuffer[255];
- |
- | CopyString( "This a test", ABuffer );
- |
- | InsertString( "is ", ABuffer, 5 );
- |
- | yields: "this is a test"
- |
- | NOTE:
- |
- | ASSUMES: There is room in the destination buffer for the
- | new insertion.
- |
- | HISTORY: 12.06.89 by Lee Malone from source by Jack A. Zucker
- | 02.15.93 changed offset to quad.
- ------------------------------------------------------------*/
- Nothing
- InsertString( AddressOfString SourceString,
- AddressOfString DestinationString,
- Quad DestinationByteOffset )
- {
- Quad SourceByteCount;
-
- SourceByteCount = CountString( SourceString );
-
- /* Make room for the new string. */
- CopyString( (AddressOfString) DestinationString+
- DestinationByteOffset,
- (AddressOfString) DestinationString+
- DestinationByteOffset+
- SourceByteCount );
-
- /* Put the new string in place. */
- CopyBytes( (AddressOfByte) SourceString,
- (AddressOfByte) DestinationString+
- DestinationByteOffset,
- SourceByteCount );
- }
-
- /*------------------------------------------------------------
- | NAME: ReplaceBytesInString
- |
- | PURPOSE: To replace all occurances of a byte within a
- | string with another.
- |
- | DESCRIPTION:
- |
- | EXAMPLE: ReplaceBytesInString( AString,
- | (Pair) "a",
- | (Pair) "A" );
- |
- | NOTE: 'Pair' used as parameter instead of 'Byte' because
- | Think C can't pass 'Byte' parameters properly. See
- | 'DataSize.h' for more.
- |
- | ASSUMES:
- |
- | HISTORY: 04.04.91 by Lee Malone
- | 11.11.93 changed 'Byte' parameters to 'Pair'.
- ------------------------------------------------------------*/
- Nothing
- ReplaceBytesInString( AddressOfString AString,
- Pair From,
- Pair To )
- {
- while(*AString)
- {
- if( *AString == (String) From )
- {
- *AString = (String) To;
- }
- AString++;
- }
- }
-